package com.csi.dao.impl;

import com.csi.dao.CommentDao;
import com.csi.domain.Comment;
import com.csi.utils.DateUtils;
import com.csi.utils.JDBCUtils;


import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CommentDaoImpl extends JDBCUtils implements CommentDao {
    DateUtils dateUtils =new DateUtils();


    @Override
    public int add(Comment comment) throws SQLException {
        final String SQL="INSERT INTO `hotel_comments` (userId,typeId,commentContent,commentDate,isAnonymity,commentGrade)VALUES(?,?,?,NOW(),?,?)";

        Connection connection = this.getConnection();

        PreparedStatement ps =connection.prepareStatement(SQL);


        ps.setInt(1,comment.getUserId());
        ps.setInt(2,comment.getTypeId());
        ps.setString(3,comment.getCommentContent());
        ps.setInt(4,comment.getIsAnonymity());
        ps.setInt(5,comment.getCommentGrade());

        int i = ps.executeUpdate();
        release(ps,connection);

        return i;
    }

    @Override
    public int delete(int commentId) throws SQLException {
        final String SQL ="DELETE FROM `hotel_comments` WHERE commentId=?";
        Connection connection = this.getConnection();

        PreparedStatement ps = connection.prepareStatement(SQL);

        ps.setInt(1,commentId);

        int i = ps.executeUpdate();

        release(ps,connection);


        return i;
    }

    @Override
    public int update(Comment comment) throws SQLException {

        final String SQL="UPDATE `hotel_comments` SET commentContent=?,commentDate=NOW(),isAnonymity=?,commentGrade=? WHERE commentId=?";

        Connection connection = this.getConnection();

        PreparedStatement ps = connection.prepareStatement(SQL);

        ps.setString(1,comment.getCommentContent());
        ps.setInt(2,comment.getIsAnonymity());
        ps.setInt(3,comment.getCommentGrade());
        ps.setInt(4,comment.getCommentId());


        int i = ps.executeUpdate();

        release(ps,connection);

        return i;
    }

    /**
     *
     * @param indexPage  起始点
     * @param title  每页条数
     * @return
     * @throws SQLException
     */
    @Override
    public List<Comment> list(int indexPage, int title) throws SQLException {
        final String SQL ="SELECT * FROM `hotel_comments` ORDER BY commentDate DESC LIMIT ?,?";

        Connection connection = this.getConnection();

        PreparedStatement ps = connection.prepareStatement(SQL);

        ps.setInt(1,indexPage);
        ps.setInt(2,title);

        ResultSet rs = ps.executeQuery();
        List<Comment> comments =new ArrayList<>();
        while (rs.next()){
            Comment comment =new Comment();
            comment.setCommentId(rs.getInt("CommentId"));
            comment.setUserId(rs.getInt("UserId"));
            comment.setTypeId(rs.getInt("TypeId"));
            comment.setCommentContent(rs.getString("CommentContent"));
            comment.setCommentDate(rs.getDate("CommentDate"));
            comment.setIsAnonymity(rs.getInt("IsAnonymity"));
            comment.setCommentGrade(rs.getInt("CommentGrade"));
            comments.add(comment);
        }
        release(rs,ps,connection);

        return comments;
    }


}
